import { constantRoutes } from '@/router'
import { getRouters,getHomeSidebarMenu } from '@/api/menu'
import Layout from '@/layout/index'

const permission = {
  state: {
    routes: [],
    addRoutes: []
  },
  mutations: {
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    },
    SET_SIDEBA_ROUTERS: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    },
    CURRENT_SIDEBAR_ROUTERS: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    },
  },
  actions: {
    // 生成路由
    GenerateRoutes({ commit }, item) {
      return new Promise(resolve => {
        // 向后端请求路由数据
        // getRouters({subsystemId:item.subsystemId}).then(res => {
        //   const accessedRoutes = filterAsyncRouter(res.data)
        //   accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
        //   commit('SET_ROUTES', accessedRoutes)
        //   resolve(accessedRoutes)
        // })
        if(item && item.subsystemId && item.openMenu == 1){
          getRouters({subsystemId:1}).then(res => {
            let shouyeRouters = [{
              component: "Layout",
              hidden: false,
              meta: {title:"审批", icon:"component", noCache: false },
              path: "/approval",
              name: 'Approval',
              url:"/approval",
              alwaysShow: true,
              children:[
                {
                  component: "approval/approvalDocManage/apply",
                  hidden: false,
                  meta: {title: "申请", icon: "checkbox", urlPath: null},
                  name: "apply",
                  path: "apply",
                  status: true,
                },
                {
                  component: "approval/approvalDocManage/examine",
                  hidden: false,
                  meta: {title: "待我审批", icon: "checkbox", urlPath: null},
                  name: "examine",
                  path: "examine",
                  status: true,
                },
                {
                  component: "approval/approvalDocManage/application",
                  hidden: false,
                  meta: {title: "我的申请", icon: "checkbox", urlPath: null},
                  name: "application",
                  path: "application",
                  status: true,
                },
                {
                  component: "approval/approvalDocManage/myApproval",
                  hidden: false,
                  meta: {title: "我的审批", icon: "checkbox", urlPath: null},
                  name: "myApproval",
                  path: "myApproval",
                  status: true,
                },
                {
                  component: "approval/approvalDocManage/comprehensive",
                  hidden: false,
                  meta: {title: "综合查询", icon: "checkbox", urlPath: null},
                  name: "comprehensive",
                  path: "comprehensive",
                  status: true,
                },
                {
                  component: "approval/approvalDocManage/apparovalProcess",
                  hidden: false,
                  meta: {title: "审批流程", icon: "checkbox", urlPath: null},
                  name: "apparovalProcess",
                  path: "apparovalProcess",
                  status: true,
                }
              ]
            }]
            let allRoutes = shouyeRouters.concat(res.data||[])
            const sdata = JSON.parse(JSON.stringify(allRoutes))
            console.log('sss', allRoutes)
            const rdata = JSON.parse(JSON.stringify(allRoutes))
            const sidebarRoutes = filterAsyncRouter(sdata)
            const rewriteRoutes = filterAsyncRouter(rdata, true)
            // console.log('444', res)
            // const accessedRoutes = filterAsyncRouter(res.data)
            // console.log('555', accessedRoutes)
            // commit('SET_ROUTES', accessedRoutes).push({ path: '*', redirect: '/404', hidden: true })
            // accessedRoutes.push({ path: '/approval', redirect: 'noRedirect', hidden: false, children:[
            //   {
            //     path: '/apply', redirect: 'noRedirect', hidden: false, 
            //   }
            // ]})
            commit('SET_ROUTES', rewriteRoutes)
            commit('SET_SIDEBA_ROUTERS', sidebarRoutes)
            // commit('CURRENT_SIDEBAR_ROUTERS', shouyeRouters[0])
            resolve(rewriteRoutes)
          })
        }
        // if(item && item.subsystemId && item.menuId && item.openMenu != 1){
        //   getHomeSidebarMenu({subsystemId:item.subsystemId, menuId:item.menuId}).then(res => {
        //     const accessedRoutes = filterAsyncRouter(res.data)
        //     accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
        //     commit('SET_ROUTES', accessedRoutes)
        //     resolve(accessedRoutes)
        //   })
        // }
      })
    }
  }
}

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
  return asyncRouterMap.filter(route => {
    if (route.component) {
      // Layout组件特殊处理
      if (route.component === 'Layout') {
        route.component = Layout
      } else {
        route.component = loadView(route.component)
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    return true
  })
}

export const loadView = (view) => { // 路由懒加载
  return (resolve) =>  require([`@/views/${view}`], resolve)
}

export default permission
